<?php
declare (strict_types=1);

namespace app\api\controller;

use addons\WeChatMini\facade\WeChatMini;
use app\common\model\Assets;
use app\common\model\Relation;
use app\common\model\User as UserModel;
use app\common\model\UserNotify;
use app\common\tool\Result;
use think\exception\ValidateException;
use think\Response;

class User extends Base
{
    public function index(): Response
    {
        $account = Assets::getFields(3);
        $assets = [];
        foreach ($account as $key => $value) {
            $assets[] = [
                'title' => $value,
                'type' => $key,
                'balance' => $this->user->assets->$key,
                'url' => "/pages/account/record?type={$key}&title={$value}"
            ];
        }
        $result = [
            'id' => $this->user->id,
            'code' => $this->user->code,
            'nickname' => $this->user->nickname,
            'mobile' => $this->user->mobile,
            'photo' => $this->user->photo,
            'level_name' => $this->user->level ? $this->user->level->title : '',
            'assets' => $assets,
            'notify_num' => UserNotify::where(['uid' => $this->user->id, 'status' => 0])->count(),
            'filing_number' => $this->system['filing_number']
        ];

        return Result::success('success', $result);
    }

    public function info(): Response
    {
        $parent = UserModel::field('id,code,nickname,mobile,photo')
            ->where(['id' => $this->user->parent_id])->find();
        $node = UserModel::field('id,code,nickname,mobile,photo')
            ->where(['id' => $this->user->node_id])->find();
        $result = [
            'id' => $this->user->id,
            'code' => $this->user->code,
            'nickname' => $this->user->nickname,
            'mobile' => $this->user->mobile,
            'photo' => $this->user->photo,
            'parent' => $parent ?: '',
            'node' => $node ?: '',
            'position' => $node ? getNodePosition($this->user->position) : '',
            'level_name' => $this->user->level ? $this->user->level->title : ''
        ];

        return Result::success('success', $result);
    }

    public function edit(): Response
    {
        $param = $this->request->post();
        try {
            $this->validate($param, 'User');
        } catch (ValidateException $e) {
            return Result::fail($e->getError());
        }

        return Result::fastReturn(UserModel::_edit($param, $this->user));
    }

    public function myTeamTotal(): Response
    {
        $result = [
            'push_count' => UserModel::where(['parent_id' => $this->user->id])->count(),
            'team_count' => Relation::where(['uid' => $this->user->id])->count(),
            'push_num' => $this->user->push_num,
            'team_num' => $this->user->team_num,
            'my_total' => $this->user->my_total,
            'team_total' => $this->user->team_total
        ];

        return Result::success('success', $result);
    }

    public function myTeamList(): Response
    {
        $sort = [
            ['key' => 'is_active', 'value' => 'desc'],
            ['key' => 'active_time', 'value' => 'asc']
        ];
        $result = UserModel::getPageList(['parent_id' => $this->user->id],
            'id,code,nickname,mobile,photo,my_total,team_total,level_id',
            $this->getPage(), $this->getLimit(), false, ['level'], [], $sort);
        foreach ($result['data'] as &$item) {
            $item->level_name = $item->level ? $item->level->title : '';
            unset($item->level);
        }

        return Result::fastReturn($result);
    }

    public function myNodeList($id = ''): Response
    {
        $field = 'id,code,nickname,mobile,photo,node_id,level_id,active_time,num_1,num_2,total_1,total_2,surplus_1,surplus_2';
        if ($id && $id != $this->user->id) {
            $user = UserModel::field($field)->where(['id' => $id])->find();
            $is_top = 0;
            $up_id = $user->node_id;
        } else {
            $user = $this->user;
            $is_top = 1;
            $up_id = 0;
        }
        $now = [
            'id' => $user->id,
            'code' => $user->code,
            'nickname' => $user->nickname,
            'mobile' => $user->mobile,
            'photo' => $user->photo,
            'active_time' => $user->active_time,
            'num_1' => $user->num_1,
            'num_2' => $user->num_2,
            'total_1' => $user->total_1,
            'total_2' => $user->total_2,
            'surplus_1' => $user->surplus_1,
            'surplus_2' => $user->surplus_2,
            'level_name' => $user->level ? $user->level->title : '',
            'is_top' => $is_top,
            'up_id' => $up_id
        ];
        $left = UserModel::field($field)->where(['node_id' => $user->id, 'position' => 1])->find();
        if ($left) {
            $left->level_name = $left->level ? $left->level->title : '';
            unset($left->level_id, $left->level);
        } else {
            $left = '';
        }
        $right = UserModel::field($field)->where(['node_id' => $user->id, 'position' => 2])->find();
        if ($right) {
            $right->level_name = $right->level ? $right->level->title : '';
            unset($right->level_id, $right->level);
        } else {
            $right = '';
        }

        return Result::fastReturn(['code' => 1, 'msg' => 'success', 'data' => $now, 'left' => $left, 'right' => $right]);
    }

    public function getInvitationCode(): Response
    {
        try {
            $version = env('APP_DEBUG') ? 'trial' : 'release';
            $result = WeChatMini::getMiniScene($this->user->code, $this->user->code, 'pages/index', '/qrcode/', $version);

            return Result::success('success', $result);
        } catch (\Exception $e) {

            return Result::fail($e->getMessage());
        }
    }
}